package com.admin.system.modules.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.admin.common.exception.BadRequestException;
import com.admin.common.utils.FileUtil;
import com.admin.common.utils.PageResult;
import com.admin.common.utils.PageUtil;
import com.admin.common.utils.StringUtils;
import com.admin.core.dto.cache.ParamConfigCacheBO;
import com.admin.core.entity.SysLocalStorage;
import com.admin.core.mapper.LocalStorageMapper;
import com.admin.system.constant.SystemConstant;
import com.admin.system.modules.system.dto.LocalStorageQueryDTO;
import com.admin.system.modules.system.service.LocalStorageService;
import com.admin.component.web.MultipartFileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @author sq-lan
 */
@Service
@RequiredArgsConstructor
public class LocalStorageServiceImpl extends ServiceImpl<LocalStorageMapper, SysLocalStorage> implements LocalStorageService {

    @Override
    public PageResult<SysLocalStorage> paging(LocalStorageQueryDTO dto) {
        PageHelper.startPage(dto.getCurrent(), dto.getSize());
        List<SysLocalStorage> list = this.list(dto);
        return PageUtil.toPageHelper(list);
    }

    @Override
    public List<SysLocalStorage> list(LocalStorageQueryDTO dto) {
        LambdaQueryWrapper<SysLocalStorage> wrapper = Wrappers.lambdaQuery();
        wrapper.like(org.springframework.util.StringUtils.hasText(dto.getBlurry()), SysLocalStorage::getName, dto.getBlurry());
        if (dto.getCreateTime() != null && !dto.getCreateTime().isEmpty()) {
            wrapper.between(SysLocalStorage::getCreateTime, dto.getCreateTime().get(0), dto.getCreateTime().get(1));
        }
        return this.list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysLocalStorage add(String name, MultipartFile multipartFile) {
        FileUtil.checkSize(ParamConfigCacheBO.fileParamConfig.getMaxSize(), multipartFile.getSize());
        String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        String type = FileUtil.getFileType(suffix);
        File file = MultipartFileUtil.upload(multipartFile, SystemConstant.otherPath() + File.separator);
        if (ObjectUtil.isNull(file)) {
            throw new BadRequestException("上传失败");
        }
        try {
            name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
            SysLocalStorage localStorage = new SysLocalStorage(
                    file.getName(),
                    name,
                    suffix,
                    file.getPath(),
                    type,
                    FileUtil.getSize(multipartFile.getSize())
            );
            save(localStorage);
            return localStorage;
        } catch (Exception e) {
            FileUtil.del(file);
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(Integer[] ids) {
        for (Integer id : ids) {
            SysLocalStorage storage = getById(id);
            FileUtil.del(storage.getPath());
            removeById(storage);
        }
    }

}
